package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.DeviceVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.DeviceConstant;
import com.bbzn.device.client.constant.RedisConstant;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.CwtingStrategy;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author wulongbo
 * @Date 2021/12/1 11:14
 * @Version 1.0
 */

@RestController
@RequestMapping("/confirm")
@Slf4j
@Api(value = "confirm", tags = "确认控制器")
public class ConfirmController extends BaseApiService {

    @Autowired
    private RedisUtils redisUtil;

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    /**
     * 确认或者取消联动
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getUserIds", method = RequestMethod.POST)
    @OperationAnnotation(content = "确认或者取消联动")
    public BaseResponse getUserIds(HttpServletRequest request, @RequestBody DeviceVO reqVO) throws Exception {
        // state 0为取消,1为确认
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(userId).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(RedisConstant.REDIS_ENUM, RedisConstant.TOKEN_CACHE_EXCEPTION_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        String imei = reqVO.getImei();
        if (!OptionalBean.ofNullable(reqVO.getImei()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_IMEI_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Integer state = reqVO.getState();
        if (!OptionalBean.ofNullable(state).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeviceConstant.DEVICE_ENUM, DeviceConstant.DEVICE_NO_STATE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        String decodeStr = Base64Util.decode(imei + "::" + userId);
        if (state == 0) {
            redisUtil.delete(decodeStr);
        } else if (state == 1) {
            String value = redisUtil.get(decodeStr);
            if (StringUtils.isEmpty(value)) {
                SystemParam systemParam = systemParamByCacheService.getValues(RedisConstant.REDIS_ENUM, RedisConstant.TOKEN_CACHE_EXCEPTION_PREFIX + language);
                throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
            }
            String[] split = value.split(".");
            CwtingStrategy cwtingStrategy = SpringUtils.getBean(split[0], CwtingStrategy.class);
            cwtingStrategy.dealAlarm(split[1], imei);
        }
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_FAIL_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }
}
